/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dameki.game;

import dameki.core.GlobalVars;
import java.util.ArrayList;

/**
 *
 * @author Tobi
 */
public class GameBoard {

    private State currentstate;

    public GameBoard() {
        currentstate = new State();
        currentstate.init();
        System.out.println("initialised...");
    }

    public void forcemove() 
    {
    }

    public void move(GameMove move) {
        currentstate.getField()[move.getStone().getPosY()][move.getStone().getPosX()] = null;
        currentstate.getField()[move.getPosY()][move.getPosX()] = move.getStone();
        move.getStone().setPosY(move.getPosY());
        move.getStone().setPosX(move.getPosX());
        if (!move.getStone().IsDame()) {
            if (move.getStone().getColor() && move.getStone().getPosY() == 7 || !move.getStone().getColor() && move.getStone().getPosY() == 0) {
                move.getStone().setDame(true);
            }
        }
    }

    public boolean checkMove(GameMove move, boolean color) {
        int col = color?1:-1;
        //pos[0] --> y
        //pos[1] --> x
        if (move.getPosY() >= 0 && move.getPosY() < 8 && move.getPosX() >= 0 && move.getPosX() < 8) {
            //if targetField is empty
            if (currentstate.getField()[move.getPosY()][move.getPosX()] == null) {
                //no jump
                if (move.getPosY() == move.getStone().getPosY() + col) {
                    if (move.getPosX() == move.getStone().getPosX() + 1 || move.getPosX() == move.getStone().getPosX() + -1) {
                        return true;
                    }
                } //jump
                else if (move.getPosY() == move.getStone().getPosY() + 2 * col) {
                    //jump to the right
                    if (move.getPosX() == move.getStone().getPosX() + 2) {
                        Stone dead = getField()[move.getPosY() - col][move.getPosX() - 1];
                        if (dead.getColor() != color) {
                            killStone(dead);
                            return true;
                        }
                        //jump to the left
                    } else if (move.getPosX() == move.getStone().getPosX() - 2) {
                        Stone dead = getField()[move.getPosY() - col][move.getPosX() + 1];
                        if (dead.getColor() != color) {
                            killStone(dead);
                            return true;
                        }
                    }
                }
                if (move.getStone().IsDame()) {
                    //no jump
                    if (move.getPosY() == move.getStone().getPosY() - col) {
                        if (move.getPosX() == move.getStone().getPosX() + 1 || move.getPosX() == move.getStone().getPosX() + -1) {
                            return true;
                        }
                    } //jump
                    else if (move.getPosY() == move.getStone().getPosY() - 2 * col) {
                        //jump to the right
                        if (move.getPosX() == move.getStone().getPosX() + 2) {
                            Stone dead = getField()[move.getPosY() + col][move.getPosX() - 1];
                            if (dead.getColor() != color) {
                                killStone(dead);
                                return true;
                            }
                            //jump to the left
                        } else if (move.getPosX() == move.getStone().getPosX() - 2) {
                            Stone dead = getField()[move.getPosY() + col][move.getPosX() + 1];
                            if (dead.getColor() != color) {
                                killStone(dead);
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        } else {
            return false;
        }
    }

    private void killStone(Stone s) {
        currentstate.removeStone(s);
    }

    public Stone[][] getField() {
        return currentstate.getField();
    }

    public ArrayList<Stone> getStoneList(boolean color) {
        return currentstate.getStoneList(color);
    }
}
